<?php

class WSB_Xtension_Form extends Zend_Form
{

	protected $_useDisplayGroups = false;

	public $elementDecorators = array(
        'ViewHelper',
        'Errors',
        array('Description', array('tag' => 'p', 'class' => 'description')),
        array(array('data' => 'HtmlTag'), array('tag' => 'dd')),
        array('Label', array('tag' => 'dt', 'requiredSuffix' => '<span class="required">*</span>', 'escape' => false))
    );

	public $hiddenDecorators = array(
        'ViewHelper',
        array(array('data' => 'HtmlTag'), array('tag' => 'dd', 'class' => 'element', 'style' => 'display:none')),
        array(array('label' => 'HtmlTag'), array('tag' => 'dt', 'style' => 'display:none', 'placement' => 'prepend'))
    );

    public $buttonDecorators = array(
        'ViewHelper',
        array('Description', array('tag' => 'p', 'class' => 'description')),
        array(array('data' => 'HtmlTag'), array('tag' => 'dd', 'class' => 'element')),
        array(array('label' => 'HtmlTag'), array('tag' => 'dt', 'style' => 'display:none', 'placement' => 'prepend'))
    );

	/*
	public $hiddenDecorators = array(
        'ViewHelper',
        //'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        array('Label', array('tag' => 'td')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr', 'style' => 'display:none')),
    );

	public $elementDecorators = array(
        'ViewHelper',
        'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        array('Label', array('tag' => 'td')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
    );

    public $buttonDecorators = array(
        'ViewHelper',
        array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        array(array('label' => 'HtmlTag'), array('tag' => 'td', 'test' => 'test', 'placement' => 'prepend')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
    );
	*/

	/**
	 * @return void
	 */
	public function useDefaultDecorators()
	{
        foreach($this->getElements() AS $element) {
			switch(get_class($element)) {
				case 'Zend_Form_Element_Hidden':
					$element->setDecorators($this->hiddenDecorators);
					break;
				case 'Zend_Form_Element_Submit':
					$element->setDecorators($this->buttonDecorators);
					break;
				default:
					$element->setDecorators($this->elementDecorators);
					break;
			}
        }

        $formName = $this->getName();

        $displayGroups = $this->getDisplayGroups();
        if(empty($displayGroups)) {
	        $this->clearDecorators();
	        $this->addDecorator('FormElements')
	             ->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => empty($formName) ? 'default-form' : $formName ))
	             ->addDecorator('Form');
        } else {
	        $this->clearDecorators();
		    $this->addDecorator('FormElements')
		         //->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => empty($formName) ? 'default-form' : 'default-form ' . $formName ))
		         ->addDecorator('Form');
        }

        $this->setDefaultDisplayGroupDecorators();
	}

	/**
	 * @return void
	 */
	public function setErrorDecoratorOptions()
	{
		$errors = $this->getErrors();
		foreach ($this->getElements() as $element) {
			if(!empty($errors[$element->getName()])) {
				$element->getDecorator('Label')->setOption('class', 'label-error');
				$element->getDecorator('HtmlTag')->setOption('class', 'element-error');
				//$element->setAttrib('class', 'error');
			}
		}
	}

    /**
     * Adds a star (*) as suffix to all label elements.
     *
     * @return void
     */
    public function addRequiredSuffixToLabels()
    {
		foreach($this->getElements() AS $element) {
			if($element instanceof Zend_Form_Element) {
				$label = $element->getDecorator('label');
				if($label instanceof Zend_Form_Decorator_Label) {
					$label->setRequiredSuffix('*');
				}
			}
		}
    }

    /**
     * Fuegt jedem Element einen Klassennamen in der Art "form-xxx" hinzu.
     * Aber nur, falls kein Klassenname definiert ist.
     *
     * @return void
     */
    public function addClassNameToElements()
    {
		foreach($this->getElements() AS $element) {
			$class = $element->getAttrib('class');
			if(empty($class)) {
				$from = array('Zend_Form_Element_', 'Eb_Form_Element_');
				$to = array('form-', 'form-');
				$class = strtolower(str_replace($from, $to, $element->getType()));
				$element->setAttrib('class', $class);
			}
		}
    }

    /**
     * @return void
     */
    public function setDefaultDisplayGroupDecorators()
    {
		foreach($this->getDisplayGroups() AS $dg) {
			$dg->clearDecorators();
            $dg->addDecorator('FormElements')
               ->addDecorator('HtmlTag', array('tag' => 'dl'))
               ->addDecorator('Fieldset');
			//$dg->removeDecorator('DtDdWrapper');
		}
    }

    /**
     * @return void
     */
    public function setDefaultSettings()
    {
    	/*
    	$displayGroups = $this->getDisplayGroups();
    	if(empty($displayGroups)) {
    	} else {
    	}
    	*/

		//$this->addRequiredSuffixToLabels();
		//$this->addClassNameToElements();
		//$this->removeDisplayGroupDecorators();
        /*$this->setDecorators(array(
            'FormElements',
            'Form'
        ));*/

        foreach($this->getElements() AS $element) {
        	if(in_array($element->getType(), array('Zend_Form_Element_Hash', 'Zend_Form_Element_Hidden'))) {
        		$element->removeDecorator('HtmlTag');
        		$element->removeDecorator('Label');
        	}
        }
    }

    /*
    public function removeElementDecorators()
    {

    }
    */

    /**
     * @return void
     */
    public function removeErrorDecorators()
    {
        foreach($this->getElements() AS $element) {
        	$element->removeDecorator('Errors');
        }
    }

    /**
     * @return boolean
     */
    public function useDisplayGroups()
    {
    	return $this->_useDisplayGroups;
    }

    /**
     * @param boolean $flag
     */
    public function setUseDisplayGroups($flag)
    {
		$this->_useDisplayGroups = (bool)$flag;
    }

}
